package com.fjh.shop.goods.service.controller;

import com.fjh.shop.common.PageResult;
import com.fjh.shop.common.Result;
import com.fjh.shop.goods.model.entity.Brand;
import com.fjh.shop.goods.service.service.BrandService;
import com.fjh.shop.user.feign.api.UserClient;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wuyunbin
 * @since 2025-07-16
 */
@Tag(name = "品牌管理", description = "品牌相关接口")
@Slf4j
@RestController
@RequestMapping("/brand")
public class BrandController {
    @Resource
    private BrandService brandService;

    @Resource
    private UserClient userClient;

    @Operation(summary = "添加品牌", description = "添加新的品牌信息")
    @ApiResponse(responseCode = "200", description = "添加成功")
    @PostMapping("")
    public Result<Object> add(@RequestBody Brand brand){
        Result<Boolean> result = userClient.checkToken();
        log.info("result:{}", result);
        brandService.save(brand);
        return Result.success();
    }
    
    /**
     * 根据ID查询品牌
     * @param id 品牌ID
     * @return 品牌信息
     */
    @Operation(summary = "根据ID查询品牌", description = "根据品牌ID查询详细信息")
    @ApiResponse(responseCode = "200", description = "查询成功")
    @GetMapping("/{id}")
    public Result<Brand> findById(@Parameter(description = "品牌ID") @PathVariable Long id) {
        log.info("根据ID查询品牌：{}", id);
        Brand brand = brandService.getById(id);
        return Result.success(brand);
    }
    
    /**
     * 分页查询品牌列表
     * @param current 页码
     * @param pageSize 每页数量
     * @param name 品牌名称(模糊查询)
     * @param status 状态
     * @return 分页结果
     */
    @Operation(summary = "分页查询品牌列表", description = "根据条件分页查询品牌列表")
    @ApiResponse(responseCode = "200", description = "查询成功")
    @GetMapping("/page")
    public PageResult<Brand> page(
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") Integer current,
            @Parameter(description = "每页数量") @RequestParam(defaultValue = "10") Integer pageSize,
            @Parameter(description = "品牌名称(模糊查询)") @RequestParam(required = false) String name,
            @Parameter(description = "状态") @RequestParam(required = false) Integer status) {
        log.info("分页查询品牌列表：current={}, pageSize={}, name={}, status={}", current, pageSize, name, status);

        return PageResult.success(brandService.pageQuery(current, pageSize, name, status));
    }
    
    /**
     * 更新品牌
     * @param brand 品牌信息
     * @return 更新结果
     */
    @Operation(summary = "更新品牌", description = "更新已有的品牌信息")
    @ApiResponse(responseCode = "200", description = "更新成功")
    @PutMapping("")
    public Result<Boolean> update(@RequestBody Brand brand) {
        log.info("更新品牌：{}", brand);
        boolean result = brandService.updateById(brand);
        return Result.success(result);
    }
    
    /**
     * 删除品牌
     * @param id 品牌ID
     * @return 删除结果
     */
    @Operation(summary = "删除品牌", description = "根据ID删除品牌")
    @ApiResponse(responseCode = "200", description = "删除成功")
    @DeleteMapping("/{id}")
    public Result<Boolean> delete(@Parameter(description = "品牌ID") @PathVariable Long id) {
        log.info("删除品牌：{}", id);
        boolean result = brandService.removeById(id);
        return Result.success(result);
    }
}
